package com.back.demo.service;

import com.alibaba.excel.EasyExcel;
import com.back.demo.controller.myVO.basicVO.ClassVO;
import com.back.demo.controller.myVO.basicVO.MajorVO;
import com.back.demo.controller.myVO.basicVO.MentorVO;
import com.back.demo.controller.myVO.adminVO.ConditionsVO;
import com.back.demo.controller.myVO.studentVO.ItemAdminVO;
import com.back.demo.controller.myVO.basicVO.StudentInformationVO;
import com.back.demo.controller.myVO.studentVO.achievementVO.*;
import com.back.demo.mapper.AdminMapper;
import com.back.demo.mapper.myDO.*;
import com.back.demo.mapper.myDO.adminDO.ConditionsDO;
import com.back.demo.mapper.myDO.basicDO.ClassDO;
import com.back.demo.mapper.myDO.basicDO.MajorDO;
import com.back.demo.mapper.myDO.basicDO.MentorDO;
import com.back.demo.mapper.myDO.basicDO.StudentInformationDO;
import com.back.demo.service.MyEasyExcel.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Slf4j
@org.springframework.stereotype.Service
public class AdminService {

    @Value("${file.upload-path}")
    private String relativeUploadPath;

    @Value("${file.allowed-types}")
    private String allowedTypes;

    @Value("${file.max-size}")
    private long maxFileSize;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private BasicService basicService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private final ObjectMapper objectMapper = new ObjectMapper();

    public String upload(MultipartFile file) throws IOException {
        String absoluteUploadPath = System.getProperty("user.dir") + File.separator + relativeUploadPath;
        if (file == null || file.isEmpty()) {
            return "文件不能为空";
        }

        if (!StringUtils.hasText(absoluteUploadPath)) {
            return "文件上传路径未配置";
        }

        if (!isFileTypeAllowed(file)) {
            return "不允许上传该文件类型";
        }

        if (!isFileContentValid(file)) {
            return "文件内容不合法";
        }

        if (file.getSize() > maxFileSize) {
            return "文件大小超过限制";
        }

        try {
            String fileName = file.getOriginalFilename();
            String filePath = absoluteUploadPath + File.separator + fileName;
            File fileDestination = new File(filePath);
            file.transferTo(fileDestination);
            return "文件上传成功";
        } catch (IOException e) {
            e.printStackTrace();
            throw new IOException("文件上传失败");
        }
    }

    private boolean isFileTypeAllowed(MultipartFile file) {
        String fileExtension = getFileExtension(file.getOriginalFilename());
        if (fileExtension != null) {
            List<String> allowedExtensions = Arrays.asList(allowedTypes.split(","));
            return allowedExtensions.contains(fileExtension.toLowerCase());
        }
        return false;
    }

    private boolean isFileContentValid(MultipartFile file) throws IOException {
        // 验证文件类型是否为图像类型
/*        String contentType = file.getContentType();
        if (contentType == null || !contentType.startsWith("image/")) {
            return false;
        }

        // 进一步验证文件内容，例如使用文件头、魔术数字等方式*/
        byte[] fileBytes = file.getBytes();

        if (fileBytes.length >= 4) {
//            byte[] jpegHeader = {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF, (byte) 0xE0};
//            byte[] pngHeader = {(byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
            byte[] xlsxHeader = {(byte) 0x50, (byte) 0x4B, (byte) 0x03, (byte) 0x04};
//            return startsWith(fileBytes, xlsxHeader) || startsWith(fileBytes, pngHeader);
            return startsWith(fileBytes, xlsxHeader);
        }

        return false;
    }

    private boolean startsWith(byte[] array, byte[] prefix) {
        if (array.length < prefix.length) {
            return false;
        }
        for (int i = 0; i < prefix.length; i++) {
            if (array[i] != prefix[i]) {
                return false;
            }
        }
        return true;
    }

    private String generateUniqueFileName(String originalFileName) {
        String fileExtension = getFileExtension(originalFileName);
        String uniqueFileName = UUID.randomUUID().toString();
        if (fileExtension != null) {
            uniqueFileName += "." + fileExtension;
        }
        return uniqueFileName;
    }

    private String getFileExtension(String fileName) {
        if (StringUtils.hasText(fileName)) {
            int dotIndex = fileName.lastIndexOf(".");
            if (dotIndex >= 0 && dotIndex < fileName.length() - 1) {
                return fileName.substring(dotIndex + 1);
            }
        }
        return null;
    }

    @Transactional
    public String importStudentInformation(String fileName) {
        String filePath = System.getProperty("user.dir") + File.separator + relativeUploadPath + File.separator + fileName;
        StudentInformationListener listener = new StudentInformationListener();
        EasyExcel.read(filePath, StudentInformationX.class, listener).sheet(0).doRead();
        ArrayList<StudentInformationX> list = listener.getList();
        for (StudentInformationX item : list) {
            boolean existed = true;
            String studentId = item.getStudentId();
            Integer userId = adminMapper.selectUserIdByStudentId(studentId);
            if (userId == null) {
                existed = false;
                // System.out.println(item);
                adminMapper.insertUser(studentId);
                userId = adminMapper.selectUserIdByStudentId(studentId);
                adminMapper.insertUserRole(userId);
                // item.setUserId(userId);
            }
            StudentInformationDO studentInformationDO = new StudentInformationDO();
            studentInformationDO.setUserId(userId);
            studentInformationDO.setStudentId(studentId);
            studentInformationDO.setName(item.getName());
            switch (item.getGender()) {
                case "女":
                    studentInformationDO.setGender(0);
                    break;
                case "男":
                    studentInformationDO.setGender(1);
                    break;
                default:
                    throw new RuntimeException("学号为" + studentId + "的性别格式错误！");
            }
            studentInformationDO.setGrade(item.getGrade());
            // 没写完
            studentInformationDO.setMajorId(item.getMajorId());
            studentInformationDO.setClassId(item.getClassId());
            // 没写完
            studentInformationDO.setMentorId(basicService.getMentorByJobId(item.getJobId()).getMentorId());
            if (!existed) {
                adminMapper.insertStudentInformation(studentInformationDO);
            } else {
                adminMapper.updateStudentInformation(studentInformationDO);
                adminMapper.updateStudentUsername(studentInformationDO);
                redisTemplate.delete(("name(username):" + studentInformationDO.getStudentId()));
                redisTemplate.delete(("user(username):" + studentInformationDO.getStudentId()));
            }
        }
        return "导入学生信息成功";
    }

    @Transactional
    public void addStudentInformation(StudentInformationVO studentInformationVO) {
        String studentId = studentInformationVO.getStudentId();
        adminMapper.insertUser(studentId);
        Integer userId = adminMapper.selectUserIdByStudentId(studentId);
        adminMapper.insertUserRole(userId);

        StudentInformationDO studentInformationDO = new StudentInformationDO();
        studentInformationDO.setUserId(userId);
        studentInformationDO.setStudentId(studentInformationVO.getStudentId());
        studentInformationDO.setName(studentInformationVO.getName());
        studentInformationDO.setGender(studentInformationVO.getGender());
        studentInformationDO.setGrade(studentInformationVO.getGrade());
        // 还没写完
        studentInformationDO.setMajorId(studentInformationVO.getMajorId());
        studentInformationDO.setClassId(studentInformationVO.getClassId());
        // 还没写完
        studentInformationDO.setMentorId(studentInformationVO.getMentorId());
        adminMapper.insertStudentInformation(studentInformationDO);
    }

    @Transactional
    public void modifyStudentInformation(StudentInformationVO studentInformationVO) {
        StudentInformationDO studentInformationDO = new StudentInformationDO();
        studentInformationDO.setUserId(studentInformationVO.getUserId());
        studentInformationDO.setStudentId(studentInformationVO.getStudentId());
        studentInformationDO.setName(studentInformationVO.getName());
        studentInformationDO.setGender(studentInformationVO.getGender());
        studentInformationDO.setGrade(studentInformationVO.getGrade());
        // 还没写完
        studentInformationDO.setMajorId(studentInformationVO.getMajorId());
        studentInformationDO.setClassId(studentInformationVO.getClassId());
        // 还没写完
        studentInformationDO.setMentorId(studentInformationVO.getMentorId());
        adminMapper.updateStudentInformation(studentInformationDO);
        adminMapper.updateStudentUsername(studentInformationDO);
    }

    public Integer countStudent(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
        return adminMapper.countStudent(conditionsDO);
    }

    public ArrayList<StudentInformationVO> acquireStudentInformation(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
        ArrayList<StudentInformationDO> listDO = adminMapper.selectStudentInformation(conditionsDO);
        ArrayList<StudentInformationVO> listVO = new ArrayList<>();
        for (StudentInformationDO studentInformationDO : listDO) {
            StudentInformationVO studentInformationVO = new StudentInformationVO();
            studentInformationVO.setUserId(studentInformationDO.getUserId());
            studentInformationVO.setStudentId(studentInformationDO.getStudentId());
            studentInformationVO.setName(studentInformationDO.getName());
            studentInformationVO.setGender(studentInformationDO.getGender());
            studentInformationVO.setGrade(studentInformationDO.getGrade());
            studentInformationVO.setMajorId(studentInformationDO.getMajorId());
            studentInformationVO.setMajorName(basicService.getMajorByMajorId(studentInformationDO.getMajorId()).getMajorName());
            studentInformationVO.setClassId(studentInformationDO.getClassId());
            studentInformationVO.setClassName(basicService.getClassByClassId(studentInformationDO.getClassId()).getClassName());
            studentInformationVO.setMentorId(studentInformationDO.getMentorId());
            studentInformationVO.setMentorName(basicService.getMentorByMentorId(studentInformationDO.getMentorId()).getMentorName());
            listVO.add(studentInformationVO);
        }
        return listVO;
    }

    public StudentInformationVO getStudentInformation(Integer userId) {
        StudentInformationDO studentInformationDO = adminMapper.selectStudentInformationByUserId(userId);
        StudentInformationVO studentInformationVO = new StudentInformationVO();
        studentInformationVO.setUserId(studentInformationDO.getUserId());
        studentInformationVO.setStudentId(studentInformationDO.getStudentId());
        studentInformationVO.setName(studentInformationDO.getName());
        studentInformationVO.setGender(studentInformationDO.getGender());
        studentInformationVO.setGrade(studentInformationDO.getGrade());
        // 还没写完！
        Integer majorId = studentInformationDO.getMajorId();
        MajorVO majorVO = basicService.getMajorByMajorId(majorId);
        studentInformationVO.setMajorId(majorId);
        studentInformationVO.setMajorCode(majorVO.getMajorCode());
        studentInformationVO.setMajorName(majorVO.getMajorName());
        Integer classId = studentInformationDO.getClassId();
        ClassVO classVO = basicService.getClassByClassId(classId);
        studentInformationVO.setClassId(classId);
        studentInformationVO.setClassName(classVO.getClassName());
        // 还没写完！
        Integer mentorId = studentInformationDO.getMentorId();
        MentorVO mentorVO = basicService.getMentorByMentorId(mentorId);
        studentInformationVO.setMentorId(mentorId);
        studentInformationVO.setJobId(mentorVO.getJobId());
        studentInformationVO.setMentorName(mentorVO.getMentorName());
        return studentInformationVO;
    }

    // gather
    // gather
    // gather
    public void gatherPaper(ConditionsVO conditionsVO) {
        String filePath = System.getProperty("user.dir") + File.separator + relativeUploadPath + File.separator + conditionsVO.getFileName();
        try {
            // 利用poi生成一个空的xlsx文件
            // 创建工作簿
            Workbook workbook = new XSSFWorkbook();
            // 创建工作表
            Sheet sheet = workbook.createSheet("学生论文成果");
            // 创建文件输出流
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            // 写入文件
            workbook.write(fileOutputStream);
            // 关闭文件输出流和工作簿
            fileOutputStream.close();
            workbook.close();
            // 查找成果
            ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
            List<PaperX> list = adminMapper.gatherPaper(conditionsDO);
            for (PaperX paperX : list) {
                MajorVO majorVO = basicService.getMajorByMajorId(paperX.getMajorId());
                paperX.setMajorCode(majorVO.getMajorCode());
                paperX.setMajorName(majorVO.getMajorName());
                ClassVO classVO = basicService.getClassByClassId(paperX.getClassId());
                paperX.setClassName(classVO.getClassName());
                MentorVO mentorVO = basicService.getMentorByMentorId(paperX.getMentorId());
                paperX.setJobId(mentorVO.getJobId());
                paperX.setMentorName(mentorVO.getMentorName());
            }
            // System.out.println(list.get(1));
            // EasyExcel.write(filePath, PaperX.class).sheet("学生成绩").doWrite(adminMapper.scoreExport());
            // 写入成果
            EasyExcel.write(filePath, PaperX.class).sheet("学生论文成果").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void gatherPatent(ConditionsVO conditionsVO) {
        String filePath = System.getProperty("user.dir") + File.separator + relativeUploadPath + File.separator + conditionsVO.getFileName();
        try {
            // 利用poi生成一个空的xlsx文件
            // 创建工作簿
            Workbook workbook = new XSSFWorkbook();
            // 创建工作表
            Sheet sheet = workbook.createSheet("学生专利成果");
            // 创建文件输出流
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            // 写入文件
            workbook.write(fileOutputStream);
            // 关闭文件输出流和工作簿
            fileOutputStream.close();
            workbook.close();
            // 查找成果
            ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
            List<PatentX> list = adminMapper.gatherPatent(conditionsDO);
            for (PatentX patentX : list) {
                MajorVO majorVO = basicService.getMajorByMajorId(patentX.getMajorId());
                patentX.setMajorCode(majorVO.getMajorCode());
                patentX.setMajorName(majorVO.getMajorName());
                ClassVO classVO = basicService.getClassByClassId(patentX.getClassId());
                patentX.setClassName(classVO.getClassName());
                MentorVO mentorVO = basicService.getMentorByMentorId(patentX.getMentorId());
                patentX.setJobId(mentorVO.getJobId());
                patentX.setMentorName(mentorVO.getMentorName());
            }
            // 写入成果
            EasyExcel.write(filePath, PatentX.class).sheet("学生专利成果").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void gatherCompetition(ConditionsVO conditionsVO) {
        String filePath = System.getProperty("user.dir") + File.separator + relativeUploadPath + File.separator + conditionsVO.getFileName();
        try {
            // 利用poi生成一个空的xlsx文件
            // 创建工作簿
            Workbook workbook = new XSSFWorkbook();
            // 创建工作表
            Sheet sheet = workbook.createSheet("学生比赛成果");
            // 创建文件输出流
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            // 写入文件
            workbook.write(fileOutputStream);
            // 关闭文件输出流和工作簿
            fileOutputStream.close();
            workbook.close();
            // 查找成果
            ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
            List<CompetitionX> list = adminMapper.gatherCompetition(conditionsDO);
            for (CompetitionX competitionX : list) {
                MajorVO majorVO = basicService.getMajorByMajorId(competitionX.getMajorId());
                competitionX.setMajorCode(majorVO.getMajorCode());
                competitionX.setMajorName(majorVO.getMajorName());
                ClassVO classVO = basicService.getClassByClassId(competitionX.getClassId());
                competitionX.setClassName(classVO.getClassName());
                MentorVO mentorVO = basicService.getMentorByMentorId(competitionX.getMentorId());
                competitionX.setJobId(mentorVO.getJobId());
                competitionX.setMentorName(mentorVO.getMentorName());
            }
            // 写入成果
            EasyExcel.write(filePath, CompetitionX.class).sheet("学生比赛成果").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void gatherSoftwareCopyright(ConditionsVO conditionsVO) {
        String filePath = System.getProperty("user.dir") + File.separator + relativeUploadPath + File.separator + conditionsVO.getFileName();
        try {
            // 利用poi生成一个空的xlsx文件
            // 创建工作簿
            Workbook workbook = new XSSFWorkbook();
            // 创建工作表
            Sheet sheet = workbook.createSheet("学生软件著作权成果");
            // 创建文件输出流
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            // 写入文件
            workbook.write(fileOutputStream);
            // 关闭文件输出流和工作簿
            fileOutputStream.close();
            workbook.close();
            // 查找成果
            ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
            List<SoftwareCopyrightX> list = adminMapper.gatherSoftwareCopyright(conditionsDO);
            for (SoftwareCopyrightX softwareCopyrightX : list) {
                MajorVO majorVO = basicService.getMajorByMajorId(softwareCopyrightX.getMajorId());
                softwareCopyrightX.setMajorCode(majorVO.getMajorCode());
                softwareCopyrightX.setMajorName(majorVO.getMajorName());
                ClassVO classVO = basicService.getClassByClassId(softwareCopyrightX.getClassId());
                softwareCopyrightX.setClassName(classVO.getClassName());
                MentorVO mentorVO = basicService.getMentorByMentorId(softwareCopyrightX.getMentorId());
                softwareCopyrightX.setJobId(mentorVO.getJobId());
                softwareCopyrightX.setMentorName(mentorVO.getMentorName());
            }            // 写入成果
            EasyExcel.write(filePath, SoftwareCopyrightX.class).sheet("学生软件著作权成果").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void gatherOther(ConditionsVO conditionsVO) {
        String filePath = System.getProperty("user.dir") + File.separator + relativeUploadPath + File.separator + conditionsVO.getFileName();
        try {
            // 利用poi生成一个空的xlsx文件
            // 创建工作簿
            Workbook workbook = new XSSFWorkbook();
            // 创建工作表
            Sheet sheet = workbook.createSheet("学生其它成果");
            // 创建文件输出流
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            // 写入文件
            workbook.write(fileOutputStream);
            // 关闭文件输出流和工作簿
            fileOutputStream.close();
            workbook.close();
            // 查找成果
            ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
            List<OtherX> list = adminMapper.gatherOther(conditionsDO);
            for (OtherX otherX : list) {
                MajorVO majorVO = basicService.getMajorByMajorId(otherX.getMajorId());
                otherX.setMajorCode(majorVO.getMajorCode());
                otherX.setMajorName(majorVO.getMajorName());
                ClassVO classVO = basicService.getClassByClassId(otherX.getClassId());
                otherX.setClassName(classVO.getClassName());
                MentorVO mentorVO = basicService.getMentorByMentorId(otherX.getMentorId());
                otherX.setJobId(mentorVO.getJobId());
                otherX.setMentorName(mentorVO.getMentorName());
            }            // 写入成果
            EasyExcel.write(filePath, OtherX.class).sheet("学生其它成果").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // gather
    // gather
    // gather
    public void download(HttpServletResponse response, String fileName) {
        String filePath = System.getProperty("user.dir") + File.separator + relativeUploadPath + File.separator + fileName;
        // 生成要下载的文件...
        response.setContentType("application/octet-stream");
        // 设置文件名,浏览器会显示下载对话框并使用该文件名
        response.setHeader("Content-Disposition", "attachment;" + filePath);
//        response.setHeader("Content-Disposition","attachment; filename=demo.xlsx");
        // 获取文件输入流
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        // 流传输
        try {
            IOUtils.copy(inputStream, response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    // add
    // add
    // add
    public Integer countPaper(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = new ConditionsDO();
        conditionsDO.setStudentId(conditionsVO.getStudentId());
        conditionsDO.setGrade(conditionsVO.getGrade());
        conditionsDO.setMajorId(conditionsVO.getMajorId());
        conditionsDO.setClassId(conditionsVO.getClassId());
        conditionsDO.setMentorId(conditionsVO.getMentorId());
        return adminMapper.countPaper(conditionsDO);
    }

    public Integer countPatent(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = new ConditionsDO();
        conditionsDO.setStudentId(conditionsVO.getStudentId());
        conditionsDO.setGrade(conditionsVO.getGrade());
        conditionsDO.setMajorId(conditionsVO.getMajorId());
        conditionsDO.setClassId(conditionsVO.getClassId());
        conditionsDO.setMentorId(conditionsVO.getMentorId());
        return adminMapper.countPatent(conditionsDO);
    }

    public Integer countCompetition(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = new ConditionsDO();
        conditionsDO.setStudentId(conditionsVO.getStudentId());
        conditionsDO.setGrade(conditionsVO.getGrade());
        conditionsDO.setMajorId(conditionsVO.getMajorId());
        conditionsDO.setClassId(conditionsVO.getClassId());
        conditionsDO.setMentorId(conditionsVO.getMentorId());
        return adminMapper.countCompetition(conditionsDO);
    }

    public Integer countSoftwareCopyright(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = new ConditionsDO();
        conditionsDO.setStudentId(conditionsVO.getStudentId());
        conditionsDO.setGrade(conditionsVO.getGrade());
        conditionsDO.setMajorId(conditionsVO.getMajorId());
        conditionsDO.setClassId(conditionsVO.getClassId());
        conditionsDO.setMentorId(conditionsVO.getMentorId());
        return adminMapper.countSoftwareCompetition(conditionsDO);
    }

    public Integer countOther(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = new ConditionsDO();
        conditionsDO.setStudentId(conditionsVO.getStudentId());
        conditionsDO.setGrade(conditionsVO.getGrade());
        conditionsDO.setMajorId(conditionsVO.getMajorId());
        conditionsDO.setClassId(conditionsVO.getClassId());
        conditionsDO.setMentorId(conditionsVO.getMentorId());
        return adminMapper.countOther(conditionsDO);
    }
    // add
    // add
    // add

    // acquire
// acquire
// acquire Integer currentPage, Integer pageSize
    public ArrayList<ItemAdminVO> acquirePaper(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
        ArrayList<PaperDO> listDO = adminMapper.selectPaper(conditionsDO);
        ArrayList<ItemAdminVO> listVO = new ArrayList<>();
        for (PaperDO paperDO : listDO) {
            ItemAdminVO itemAdminVO = new ItemAdminVO();
            itemAdminVO.setStudentId(paperDO.getStudentId());
            itemAdminVO.setName(paperDO.getName());
            itemAdminVO.setItem(paperDO.getPaperTitle());
            itemAdminVO.setId(paperDO.getPaperId());
            listVO.add(itemAdminVO);
        }
        return listVO;
    }

    public ArrayList<ItemAdminVO> acquirePatent(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
        ArrayList<PatentDO> listDO = adminMapper.selectPatent(conditionsDO);
        ArrayList<ItemAdminVO> listVO = new ArrayList<>();
        for (PatentDO patentDO : listDO) {
            ItemAdminVO itemAdminVO = new ItemAdminVO();
            itemAdminVO.setStudentId(patentDO.getStudentId());
            itemAdminVO.setName(patentDO.getName());
            itemAdminVO.setItem(patentDO.getPatentName());
            itemAdminVO.setId(patentDO.getPatentId());
            listVO.add(itemAdminVO);
        }
        return listVO;
    }

    public ArrayList<ItemAdminVO> acquireCompetition(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
        ArrayList<CompetitionDO> listDO = adminMapper.selectCompetition(conditionsDO);
        ArrayList<ItemAdminVO> listVO = new ArrayList<>();
        for (CompetitionDO competitionDO : listDO) {
            ItemAdminVO itemAdminVO = new ItemAdminVO();
            itemAdminVO.setStudentId(competitionDO.getStudentId());
            itemAdminVO.setName(competitionDO.getName());
            itemAdminVO.setItem(competitionDO.getCompetitionName());
            itemAdminVO.setId(competitionDO.getCompetitionId());
            listVO.add(itemAdminVO);
        }
        return listVO;
    }

    public ArrayList<ItemAdminVO> acquireSoftwareCopyright(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
        ArrayList<SoftwareCopyrightDO> listDO = adminMapper.selectSoftwareCopyright(conditionsDO);
        ArrayList<ItemAdminVO> listVO = new ArrayList<>();
        for (SoftwareCopyrightDO softwareCopyrightDO : listDO) {
            ItemAdminVO itemAdminVO = new ItemAdminVO();
            itemAdminVO.setStudentId(softwareCopyrightDO.getStudentId());
            itemAdminVO.setName(softwareCopyrightDO.getName());
            itemAdminVO.setItem(softwareCopyrightDO.getShortForm());
            itemAdminVO.setId(softwareCopyrightDO.getSoftwareCopyrightId());
            listVO.add(itemAdminVO);
        }
        return listVO;
    }

    public ArrayList<ItemAdminVO> acquireOther(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = generateConditionsDO(conditionsVO);
        ArrayList<OtherDO> listDO = adminMapper.selectOther(conditionsDO);
        ArrayList<ItemAdminVO> listVO = new ArrayList<>();
        for (OtherDO otherDO : listDO) {
            ItemAdminVO itemAdminVO = new ItemAdminVO();
            itemAdminVO.setStudentId(otherDO.getStudentId());
            itemAdminVO.setName(otherDO.getName());
            itemAdminVO.setItem(otherDO.getOtherName());
            itemAdminVO.setId(otherDO.getOtherId());
            itemAdminVO.setTip(otherDO.getType());
            listVO.add(itemAdminVO);
        }
        return listVO;
    }

    private ConditionsDO generateConditionsDO(ConditionsVO conditionsVO) {
        ConditionsDO conditionsDO = new ConditionsDO();
        conditionsDO.setStudentId(conditionsVO.getStudentId());
        conditionsDO.setGrade(conditionsVO.getGrade());
        conditionsDO.setMajorId(conditionsVO.getMajorId());
        conditionsDO.setClassId(conditionsVO.getClassId());
        conditionsDO.setMentorId(conditionsVO.getMentorId());
        conditionsDO.setPageSize(conditionsVO.getPageSize());
        conditionsDO.setStartRow((conditionsVO.getCurrentPage() - 1) * conditionsVO.getPageSize());
        return conditionsDO;
    }
    // acquire
    // acquire
    // acquire

    // find
// find
// find
    public PaperVO findPaperDetail(Integer paperId) {
        PaperDO paperDO = adminMapper.selectPaperDetail(paperId);
        PaperVO paperVO = new PaperVO();
        paperVO.setPaperId(paperDO.getPaperId());
        paperVO.setPaperTitle(paperDO.getPaperTitle());
        paperVO.setPaperAbstract(paperDO.getPaperAbstract());
        paperVO.setPaperKeyword(paperDO.getPaperKeyword());
        paperVO.setType(paperDO.getType());
        paperVO.setDate(paperDO.getDate());
        paperVO.setAuthor(paperDO.getAuthor());
        paperVO.setOrder(paperDO.getOrder());
        paperVO.setAward(paperDO.getAward());
        paperVO.setFilePath(paperDO.getFilePath());
        paperVO.setStudentId(paperDO.getStudentId());
        paperVO.setName(paperDO.getName());
        return paperVO;
    }

    public PatentVO findPatentDetail(Integer patentId) {
        PatentDO patentDO = adminMapper.selectPatentDetail(patentId);
        PatentVO patentVO = new PatentVO();
        patentVO.setPatentId(patentDO.getPatentId());
        patentVO.setPatentName(patentDO.getPatentName());
        patentVO.setPatentIntroduction(patentDO.getPatentIntroduction());
        patentVO.setType(patentDO.getType());
        patentVO.setApplicationDate(patentDO.getApplicationDate());
        patentVO.setPublicationDate(patentDO.getPublicationDate());
        patentVO.setApplicationNumber(patentDO.getApplicationNumber());
        patentVO.setPublicationNumber(patentDO.getPublicationNumber());
        patentVO.setStudentId(patentDO.getStudentId());
        patentVO.setName(patentDO.getName());
        return patentVO;
    }

    public CompetitionVO findCompetitionDetail(Integer competitionId) {
        CompetitionDO competitionDO = adminMapper.selectCompetitionDetail(competitionId);
        CompetitionVO competitionVO = new CompetitionVO();
        competitionVO.setCompetitionId(competitionDO.getCompetitionId());
        competitionVO.setCompetitionName(competitionDO.getCompetitionName());
        competitionVO.setAward(competitionDO.getAward());
        competitionVO.setLevel(competitionDO.getLevel());
        competitionVO.setSponsor(competitionDO.getSponsor());
        competitionVO.setDate(competitionDO.getDate());
        competitionVO.setSpot(competitionDO.getSpot());
        competitionVO.setDescription(competitionDO.getDescription());
        competitionVO.setStudentId(competitionDO.getStudentId());
        competitionVO.setName(competitionDO.getName());
        return competitionVO;
    }

    public SoftwareCopyrightVO findSoftwareCopyrightDetail(Integer softwareCopyrightId) {
        SoftwareCopyrightDO softwareCopyrightDO = adminMapper.selectSoftwareCopyrightDetail(softwareCopyrightId);
        SoftwareCopyrightVO softwareCopyrightVO = new SoftwareCopyrightVO();
        softwareCopyrightVO.setSoftwareCopyrightId(softwareCopyrightDO.getSoftwareCopyrightId());
        softwareCopyrightVO.setFullName(softwareCopyrightDO.getFullName());
        softwareCopyrightVO.setShortForm(softwareCopyrightDO.getShortForm());
        softwareCopyrightVO.setVersion(softwareCopyrightDO.getVersion());
        softwareCopyrightVO.setOwner(softwareCopyrightDO.getOwner());
        softwareCopyrightVO.setCode(softwareCopyrightDO.getCode());
        softwareCopyrightVO.setStatus(softwareCopyrightDO.getStatus());
        softwareCopyrightVO.setDescription(softwareCopyrightDO.getDescription());
        softwareCopyrightVO.setDate(softwareCopyrightDO.getDate());
        softwareCopyrightVO.setMethod(softwareCopyrightDO.getMethod());
        softwareCopyrightVO.setSoftwareCopyrightId(softwareCopyrightDO.getSoftwareCopyrightId());
        softwareCopyrightVO.setStudentId(softwareCopyrightDO.getStudentId());
        softwareCopyrightVO.setName(softwareCopyrightDO.getName());
        return softwareCopyrightVO;
    }

    public OtherVO findOtherDetail(Integer otherId) {
        OtherDO otherDO = adminMapper.selectOtherDetail(otherId);
        OtherVO otherVO = new OtherVO();
        otherVO.setOtherId(otherDO.getOtherId());
        otherVO.setOtherName(otherDO.getOtherName());
        otherVO.setType(otherDO.getType());

        // LocalDate 对象
        LocalDate localDate = otherDO.getDate();

        // 创建一个 DateTimeFormatter 对象，指定输出格式
        // 例如，这里我们使用 "yyyy-MM-dd" 格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 使用 DateTimeFormatter 将 LocalDate 转换为字符串
        String date = localDate.format(formatter);

        otherVO.setDate(date);
        otherVO.setDescription(otherDO.getDescription());
        otherVO.setFileName(otherDO.getFileName());
        otherVO.setStudentId(otherDO.getStudentId());
        otherVO.setName(otherDO.getName());

        return otherVO;
    }

    // find
    // find
    // find

    // major/class/mentor
    // major/class/mentor
    // major/class/mentor
    public void addMajorInformation(MajorVO majorVO) {
        MajorDO majorDO = new MajorDO();
        majorDO.setMajorCode(majorVO.getMajorCode());
        majorDO.setMajorName(majorVO.getMajorName());
        redisTemplate.delete("majorVOListJson");
        adminMapper.insertMajor(majorDO);
    }

    public void modifyMajorInformation(MajorVO majorVO) {
        MajorDO majorDO = new MajorDO();
        majorDO.setMajorId(majorVO.getMajorId());
        majorDO.setMajorCode(majorVO.getMajorCode());
        majorDO.setMajorName(majorVO.getMajorName());
        redisTemplate.delete("majorVOListJson");
        redisTemplate.delete("majorVO(majorId):" + majorVO.getMajorId());
        adminMapper.updateMajor(majorDO);
    }

    public void removeMajorInformation(Integer majorId) {
        adminMapper.removeMajor(majorId);
        redisTemplate.delete("majorVOListJson");
        redisTemplate.delete("majorVO(majorId):" + majorId);
    }

    public void addClassInformation(ClassVO classVO) {
        ClassDO classDO = new ClassDO();
        classDO.setClassOrder(classVO.getClassOrder());
        classDO.setClassName(classVO.getClassName());
        redisTemplate.delete("classVOListJson");
        adminMapper.insertClass(classDO);
    }

    @Transactional
    public void moveClass(Integer classId1, Integer classId2) {
        adminMapper.moveClassUp(classId2);
        adminMapper.moveClassDown(classId1);
        redisTemplate.delete("classVOListJson");
        redisTemplate.delete("classVO(classId):" + classId1);
        redisTemplate.delete("classVO(classId):" + classId2);

    }

    public void modifyClassInformation(ClassVO classVO) {
        ClassDO classDO = new ClassDO();
        classDO.setClassId(classVO.getClassId());
        classDO.setClassOrder(classVO.getClassOrder());
        classDO.setClassName(classVO.getClassName());
        redisTemplate.delete("classVOListJson");
        redisTemplate.delete("classVO(classId):" + classVO.getClassId());
        adminMapper.updateClass(classDO);
    }

    public void removeClassInformation(Integer classId) {
        adminMapper.removeClass(classId);
        redisTemplate.delete("classVOListJson");
        redisTemplate.delete("classVO(classId):" + classId);
    }

    public void addMentorInformation(MentorVO mentorVO) {
        MentorDO mentorDO = new MentorDO();
        mentorDO.setJobId(mentorVO.getJobId());
        mentorDO.setMentorName(mentorVO.getMentorName());
        redisTemplate.delete("mentorVOListJson");
        adminMapper.insertMentor(mentorDO);
    }

    public void modifyMentorInformation(MentorVO mentorVO) {
        MentorDO mentorDO = new MentorDO();
        mentorDO.setMentorId(mentorVO.getMentorId());
        mentorDO.setJobId(mentorVO.getJobId());
        mentorDO.setMentorName(mentorVO.getMentorName());
        redisTemplate.delete("mentorVOListJson");
        redisTemplate.delete("mentorVO(mentorId):" + mentorVO.getMentorId());
        adminMapper.updateMentor(mentorDO);
    }

    public void removeMentorInformation(Integer mentorId) {
        adminMapper.removeMentor(mentorId);
        redisTemplate.delete("mentorVOListJson");
        redisTemplate.delete("mentorVO(mentorId):" + mentorId);
    }

    // major/class/mentor
    // major/class/mentor
    // major/class/mentor

/*    public static void main(String[] args) {
        AdminService adminService = new AdminService();
        adminService.acquireMentor();
        for (MentorVO mentor : adminService.acquireMentor()) {
            System.out.println(mentor);
        }
    }*/
}
